package com.boot2.core.model;

import com.boot2.core.LongBaseEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;

/**
 * @author zhangweilin
 * @ClassName: SchedulerTask
 * @Description: 【定时任务】 model
 * @date 2018-6-4 17:48:41
 */
@Data
@ToString(callSuper = true)
@Entity
@Table(name = "scheduler_task")
@SQLDelete(sql = "update scheduler_task set is_deleted = 1 where id = ?")
@Where(clause = "is_deleted = 0")
@DynamicInsert // 加上此注解，可以只添加有值的字段
@DynamicUpdate // 此注解，是指，会先查，然后跟参数对比，不一样的就更新，所以并不是有值更新，如果db中有值，参数里没值，那么db中的值会清空
//@Cacheable
//@Cache(usage = CacheConcurrencyStrategy.TRANSACTIONAL)
public class SchedulerTask extends LongBaseEntity {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public SchedulerTask() {
        super();
    }

    public SchedulerTask(Long id) {
        this.id = id;
    }

    /**
     * @param isInit 是否初始化基础信息
     */
    public SchedulerTask(boolean isInit) {
        super(isInit);
    }
    /**
     * 定时规则id
     */
//	private Long schedulerRuleId;

    /**
     * 定时规则id
     * ManyToOne：多对一的配置
     * cascade(级联)：all(所有)，merge(更新)，refresh(查询)，persistence(保存)，remove(删除) fetch:
     * eager:立即加载 one的一方默认是立即加载 lazy:懒加载 many的一方默认是懒加载 optional:是否可选,外键是否允许为空
     * <p>
     * JoinColumn:指定外键名
     */
////    @JsonIgnore // jackson不序列化
////    @JSONField(serialize = false) // fast不序列化
//    @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
//    @JoinColumn(name = "scheduler_rule_id", nullable = true)
//    @NotFound(action = NotFoundAction.IGNORE)
//    private SchedulerRule schedulerRule;

    /**
     * 任务的时间表达式
      */
    @ApiModelProperty(value = "任务的时间表达式")
    private String cronExpression;

    /**
     * 任务名称
     */
    @ApiModelProperty(value = "任务名称")
    private String name;

    /**
     * 对应的app名
     */
    @ApiModelProperty(value = "对应的app名")
    private String appName;

    /**
     * 对应的类型字符串
     */
    @ApiModelProperty(value = "对应的类型字符串")
    private String code;

    /**
     * 对应的参数，建议用json格式
     */
    @ApiModelProperty(value = "对应的参数，建议用json格式")
    private String paramJson;

    /**
     * 处理的实现类
     */
    @ApiModelProperty(value = "处理的实现类")
    private String className;


    /**
     * 是否并发执行
     */
    @ApiModelProperty(value = "是否并发执行")
    private Boolean isConcurrent=true;
    /**
     * 当运行失败时尝试次数 (1到3)
     */
    @ApiModelProperty(value = "当运行失败时尝试次数 (1到3)")
    private Integer retryTimes=0;
    /**
     * 备注
     */
    @ApiModelProperty(value = "备注")
    private String remark;
    /**
     * 是否启用
     */
    @ApiModelProperty(value = "是否启用")
    private Boolean isEnabled=true;

    /**
     * 关联的业务主键id
     */
    @ApiModelProperty(value = "关联的业务的主键id")
    private String bussinessId;

    /**
     * 调度触发时间
     */
    @ApiModelProperty(value = "调度触发时间")
    private Date scheduledFireTime;

    /**
     * 上次触发时间
     */
    @ApiModelProperty(value = "上次触发时间")
    private Date previousFireTime;

    /**
     * 下次触发时间
     */
    @ApiModelProperty(value = "下次触发时间")
    private Date nextFireTime;

    /**
     * 被触发次数
     */
    @ApiModelProperty(value = "被触发次数")
    private int fireCount;

    /**
     * 本次触发时间
     */
    @ApiModelProperty(value = "本次触发时间")
    private Date currentFireTime;

    /**
     * 当前task在quartz中的状态
     * NONE, NORMAL, PAUSED, COMPLETE, ERROR, BLOCKED
     * ,其中,NONE拆分成两种情况：即NONE_NO_RULE和NONE
     */
    @Transient
    private String triggerState;

    @Transient
    private String triggerStateText;

    public String getTriggerStateText() {
        if ("NONE".equals(triggerState)) {
            return "未启用(可启用)";
        } else if ("NONE_NO_RULE".equals(triggerState)) {
            return "未启用(配置规则后可启用)";
        }else if ("NORMAL".equals(triggerState)) {
            return "被调度中";
        }else if ("PAUSED".equals(triggerState)) {
            return "已暂停";
        }else if ("COMPLETE".equals(triggerState)) {
            return "已完成";
        }else if ("ERROR".equals(triggerState)) {
            return "错误";
        }else if ("BLOCKED".equals(triggerState)) {
            return "阻塞";
        }else {
            return "未知";
        }
    }


    @Override
    public String toString() {
        return "SchedulerTask{" +
                ", name='" + name + '\'' +
                ", code='" + code + '\'' +
                ", remark='" + remark + '\'' +
                ", isEnabled=" + isEnabled +
                ", id=" + id +
                ", createTime=" + createTime +
                ", createTime1=" + createTime1 +
                ", createTime2=" + createTime2 +
                ", updateTime=" + updateTime +
                ", createrName='" + createrName + '\'' +
                ", updaterName='" + updaterName + '\'' +
                ", isDeleted=" + isDeleted +
                '}';
    }
}
